package com.zcc.makingfriend.service.impl;

import com.zcc.makingfriend.dao.Category;
import com.zcc.makingfriend.dto.PageDto;
import com.zcc.makingfriend.mapper.CategoryMapper;
import com.zcc.makingfriend.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author zcc
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public int addCategory(Category category) {
        List<Category> allCategory = findAllCategory();
        // 返回-1表示标题已经重复
        for (Category element : allCategory) {
            if (Objects.equals(element.getTitle(), category.getTitle())) {
                return -1;
            }
        }
        category.setUuid(UUID.randomUUID().toString());
        category.setStatus(0);
        return categoryMapper.addCategory(category);
    }

    @Override
    public int deleteCategory(Long id) {
        return categoryMapper.deleteCategory(id);
    }

    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    @Override
    public List<Category> findAllCategory() {
        return categoryMapper.findAllCategory();
    }

    @Override
    public List<Category> findCategory() {
        return categoryMapper.findCategory();
    }

    @Override
    public List<Category> findCategoryByPage(int page, int size) {
        List<Category> categoryByPage = categoryMapper.findCategoryByPage((page - 1) * size, size);
        return categoryByPage;
    }

    @Override
    public List<Category> findCategoryByCondition(PageDto pageDto) {
        pageDto.setPage((pageDto.getPage() - 1) * pageDto.getSize());
        return categoryMapper.findCategoryByCondition(pageDto);
    }

    @Override
    public Category findCategoryByTitle(String title) {
        return categoryMapper.findCategoryByTitle(title);
    }

    @Override
    public Category findCategoryByUuid(String uuid) {
        return categoryMapper.findCategoryByUuid(uuid);
    }

    @Override
    public Long countCategory() {
        return categoryMapper.countCategory();
    }

    @Override
    public Long countCategoryByCondition(PageDto pageDto) {
        return categoryMapper.countCategoryByCondition(pageDto);
    }
}
